package com.albert.order.entry;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author yangjunwei
 * @date 2021/7/3 上午2:06
 */
public class DelayOrderTask implements Delayed {

    /**
     * 订单id
     */
    private Integer orderId;

    /**
     * 限时时间
     */
    private Long timeLimitValue;
    /**
     * 创建时间
     */
    private long startTime = System.currentTimeMillis();

    public DelayOrderTask(Integer orderId, Long timeLimitValue) {
        this.orderId = orderId;
        this.timeLimitValue = timeLimitValue;
    }

    public Integer getOrderId() {
        return orderId;
    }

    public Long getTimeLimitValue() {
        return timeLimitValue;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        //(预期到期时间 - 当前时间)递减
        return unit.convert((startTime + timeLimitValue ) - System.currentTimeMillis(), unit);
    }

    @Override
    public int compareTo(Delayed delayed) {
        long value = this.getDelay(TimeUnit.MILLISECONDS) - delayed.getDelay(TimeUnit.MILLISECONDS);
        if (value == 0) {
            return 0;
        }
        if (value > 0) {
            return 1;
        }
        return -1;
    }
}
